package com.yeook.ymax.ui.activitys;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.content.Loader;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;

import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.lib.SlidingMenu;
import com.yeook.ymax.R;
import com.yeook.ymax.service.MainService;
import com.yeook.ymax.service.MainService.LocalBinder;
import com.yeook.ymax.ui.entity.RingtoneView;
import com.yeook.ymax.ui.fragments.ImageMenuFragment;
import com.yeook.ymax.ui.fragments.RingtoneListFragment;
import com.yeook.ymax.ui.fragments.RingtoneMenuFragment;

public class RingtoneUIActivity extends AbsListViewBaseActivity {
	private MainService service;
	private Fragment mContent;
	
	public static ArrayList<RingtoneView> ringtoneViews = new ArrayList<RingtoneView>();
	
	public Loader<Cursor> ringtoneLoader;
	
	public int cateCode = 1;
	
	private final ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			
			RingtoneUIActivity.this.service  = ((LocalBinder) service).getService();
			//加载铃声XML
			RingtoneUIActivity.this.service.loadRingtoneXML();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			
		}
		
	};
	
	private final BroadcastReceiver statusReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if(ringtoneLoader != null && ringtoneViews.size() == 0){
				ringtoneLoader.forceLoad();
			}
			System.out.println("Status broadcast received");
			
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(R.string.app_name);

		setContentView(R.layout.ringtone_content_frame);

		// check if the content frame contains the menu frame
		if (findViewById(R.id.menu_frame) == null) {
			setBehindContentView(R.layout.menu_frame);
			getSlidingMenu().setSlidingEnabled(true);
			getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
			// show home as up so we can toggle
			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
			getSupportActionBar().setLogo(R.drawable.lib_menu);
			getSupportActionBar().setDisplayUseLogoEnabled(true);
		} else {
			// add a dummy view
			View v = new View(this);
			setBehindContentView(v);
			getSlidingMenu().setSlidingEnabled(false);
			getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		}

		// set the Above View Fragment
		if (savedInstanceState != null){
			//mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
		}
		
		if (mContent == null){	
			mContent = new RingtoneListFragment(1, this.getString(R.string.ringtone_category_1));
		}
		
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.content_frame, mContent)
		.commit();

		// set the Behind View Fragment
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.menu_frame, new RingtoneMenuFragment())
		.commit();

		// customize the SlidingMenu
		SlidingMenu sm = getSlidingMenu();
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindScrollScale(0.25f);
		sm.setFadeDegree(0.25f);
		
		registerReceiver(statusReceiver, new IntentFilter("status"));
		
		Intent i = new Intent(this, MainService.class);
		startService(i);
		bindService(i, conn, Context.BIND_AUTO_CREATE);
	}
	
	@Override
	protected void onDestroy() {
		if (service != null) {
			unbindService(conn);
			unregisterReceiver(statusReceiver);
		}
		
		RingtoneUIActivity.ringtoneViews.clear();
		
		super.onDestroy();
	}
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle();
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		getSupportFragmentManager().putFragment(outState, "mContent", mContent);
	}


	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		getMenuInflater().inflate(R.menu.main_menu, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	public void switchContent(final Fragment fragment) {
		mContent = fragment;
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.content_frame, fragment)
		.commit();
		Handler h = new Handler();
		h.postDelayed(new Runnable() {
			public void run() {
				getSlidingMenu().showContent();
			}
		}, 50);
	}
	
	public void startRingtoneEditActivity(int position) {
		
		//Intent intent = ImagePagerActivity.newInstance(this, position, ringtoneViews);
		//startActivity(intent);
	}
}
